from time import sleep

import selenium
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
class conn():
    def __init__(self,browser):
        self.browser = browser
        print(self.browser)
    def url_conn(self,conn_url):
        # chrome_options.add_argument('--headless')
        # chrome_options.add_argument('--disable-gpu')
        # chrome_options.add_argument('--no-sandbox')
        # chrome_options.add_argument('--hide-scrollbars')
        browser = self.browser
        browser.implicitly_wait(5)
        browser.get(conn_url)

    def find_by_id(self,id):
        browser = self.browser
        WebDriverWait(browser, 10).until(EC.visibility_of(browser.find_element_by_id(id)))
        return browser.find_element_by_id(id)

    def find_by_xpath(self, xpath):
        browser = self.browser
        WebDriverWait(browser, 10).until(EC.visibility_of(browser.find_element_by_xpath(xpath)))
        return browser.find_element_by_xpath(xpath)

    def long_wait(self,xpath):
        browser = self.browser
        try:
            WebDriverWait(browser, 10).until(EC.visibility_of(browser.find_element_by_xpath(xpath)))
            element = browser.find_element_by_xpath(xpath)
            webdriver.ActionChains(browser).move_to_element(element).click(element).perform()
            print(111)
        except :
            print('超时，但是我不信找不到')
            self.long_wait(xpath)


    def find_by_text(self, text):
        browser = self.browser
        WebDriverWait(browser, 10).until(EC.visibility_of(browser.find_element_by_link_text(text)))
        return browser.find_element_by_link_text(text)

    def find_by_css(self,css):
        browser = self.browser
        WebDriverWait(browser, 10).until(EC.visibility_of(browser.find_elements_by_css_selector(css)))
        return browser.find_elements_by_css_selector(css)

    def find_by_xpath_click(self,css):
        browser = self.browser
        WebDriverWait(browser, 10).until(EC.visibility_of(browser.find_element_by_xpath(css)))
        element = browser.find_element_by_xpath(css)
        webdriver.ActionChains(browser).move_to_element(element).click(element).perform()

    def find_by_css_click(self,css):
        browser = self.browser
        WebDriverWait(browser, 10).until(EC.visibility_of(browser.find_elements_by_css_selector(css)))
        element = browser.find_elements_by_css_selector(css)
        webdriver.ActionChains(browser).move_to_element(element).click(element).perform()


if __name__ == '__main__':
    a = conn(browser=1)